package com.cn.lp.export.plugin.common;

import com.cn.lp.export.log.LogHandler;
import com.cn.lp.export.plugin.common.log.MavenLocalLoggerFactory;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;

import java.util.List;

//@Mojo()
public abstract class CommonMojo<T extends ExportTask> extends AbstractMojo {

    protected abstract List<T> getTaskList();

    protected abstract Process getProcess(T task);

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        MavenLocalLoggerFactory loggerFactory = new MavenLocalLoggerFactory(this);
        LogHandler.changeFactory(loggerFactory);
        for (T task : getTaskList()) {
            try {
                getLog().info("执行任务 : " + task.getId());
                Process process = getProcess(task);
                process.execute();
                getLog().info("执行任务 : " + task.getId() + " 完毕");
            } catch (Exception e) {
                getLog().error(task.getId() + " 任务执行失败", e);
            }
        }
    }
}
